package com.open.party.cloud.view.task

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.task.TaskDetailEntity
import com.open.party.cloud.model.task.TaskRateEntity
import com.open.party.cloud.view.comm.utils.CommonUtils
import com.open.party.cloud.view.task.adapter.TaskRateAdapter
import com.open.party.cloud.viewModel.TaskViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import kotlinx.android.synthetic.main.activity_task_detail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 任务详情
 */
class TaskDetailActivity : AppEventBusTitleBaseActivity() {
    var id = ""
    var fromWhere = ""

    private var taskDetailEntity: TaskDetailEntity? = null
    private var taskRateAdapter: TaskRateAdapter? = null
    var viewModel: TaskViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_task_detail


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("任务详情")

        init()
        initListener()
    }

    private fun init() {
        id = intent.getStringExtra("id") as String
        fromWhere = intent.getStringExtra("fromWhere") as String

//        val isFinish = intent.getBooleanExtra("isFinish", false)
//        if (isFinish) {
//            ll.visibility = View.GONE
//        } else {
//            ll.visibility = View.VISIBLE
//        }
        taskRateAdapter = TaskRateAdapter(this)
        rateListView.adapter = taskRateAdapter
        viewModel = TaskViewModel()
    }

    override fun onResume() {
        super.onResume()
        viewModel!!.loadTaskDetail(id)
    }

    private fun initListener() {

        reportRateBtn.setOnClickListener {
            // 新增进度
            XUtils.intent().openActivity(this@TaskDetailActivity, ReportRateActivity::class.java)
                .putStringExtra("id", taskDetailEntity!!.taskVO.id)
                .start()
        }

        checkTaskBtn.setOnClickListener {
            XUtils.intent().openActivity(this@TaskDetailActivity, TaskCheckAddActivity::class.java)
                .putStringExtra("id", id)
                .start()
        }

//        finishTaskBtn.setOnClickListener {
//            XUtils.intent().openActivity(this@TaskDetailActivity, ReportRateActivity::class.java)
//                .putStringExtra("id", taskDetailEntity!!.taskVO.id)
//                .putBooleanExtra("isFinish", true)
//                .start()
//        }

        taskRateAdapter!!.setOnAdapterItemClickListener { _, data ->
            // 查看
            XUtils.intent().openActivity(this@TaskDetailActivity, RateDetailActivity::class.java)
                .putStringExtra("id", data.id)
                .start()
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun updateRate(taskRateEntity: TaskRateEntity) {
        val list = ArrayList<TaskRateEntity>()
        list.add(taskRateEntity)
//        taskRateTv.text = "最新进度:${taskRateEntity.rate}%"
        taskRateAdapter!!.addData(list)

//        if (taskRateEntity.rate == 100) {
//            ll.visibility = View.GONE
//        } else {
//            ll.visibility = View.VISIBLE
//        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<TaskDetailEntity>) {

        if ("loadTaskDetail" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                taskDetailEntity = result.data as TaskDetailEntity
                showData(taskDetailEntity!!)
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showData(taskDetailEntity: TaskDetailEntity) {
        taskNameTv.text = taskDetailEntity.taskVO.title

        if (taskDetailEntity.taskVO.state != null) {
            CommonUtils.setSpanContentColor(
                this,
                taskStateTv,
                "任务状态: ",
                R.color.color_B3B3B3,
                taskDetailEntity.taskVO.state.desc,
                R.color.black
            )
        }

        if (taskDetailEntity.taskVO.endTime != null) {
            CommonUtils.setSpanContentColor(
                this,
                taskFinishDateTv,
                "完成时间: ",
                R.color.color_B3B3B3,
                XUtils.date().getDateStrByDate(taskDetailEntity.taskVO.endTime, "yyyy年MM月dd日"),
                R.color.black
            )
        }

        if (taskDetailEntity.taskVO.urgencyType != null) {
            CommonUtils.setSpanContentColor(
                this,
                taskUrgencyStateTv,
                "紧急程度: ",
                R.color.color_B3B3B3,
                taskDetailEntity.taskVO.urgencyType.desc,
                R.color.black
            )
        }

        if (XUtils.string().isNotEmpty(taskDetailEntity.taskVO.pushUserName)) {
            CommonUtils.setSpanContentColor(
                this,
                taskPushUserNameTv,
                "发  布  人:",
                R.color.color_B3B3B3,
                XUtils.string().getNotNullValue(taskDetailEntity.taskVO.pushUserName, "未知"),
                R.color.black
            )
        }

//        CommonUtils.setSpanContentColor(
//            this,
//            taskRateTv,
//            "最新进度: ",
//            R.color.color_B3B3B3,
//            taskDetailEntity.taskVO.rate.toString() + "%",
//            R.color.black
//        )

//        if (taskDetailEntity.taskVO.rate == 100) {
//            ll.visibility = View.GONE
//        } else {
//            ll.visibility = View.VISIBLE
//        }

        var statusCode = ""
        if (taskDetailEntity.taskVO.state != null) {
            statusCode = taskDetailEntity.taskVO.state.code

            // 审核/汇报进度
            if ("receive" == fromWhere) {
                // 我收到的 -> 汇报进度
                if ("JXZ" == statusCode || "YTH" == statusCode || "YYQ" == statusCode) {
                    // JXZ("进行中"),YTH("已退回"),YYQ("已延期"),
                    reportRateBtn.visibility = View.VISIBLE
                } else {
                    reportRateBtn.visibility = View.GONE
                }
            } else {
                // 我发出的 -> 进度审核
                if ("SHZ" == statusCode) {
                    checkTaskBtn.visibility = View.VISIBLE
                } else {
                    checkTaskBtn.visibility = View.GONE
                }
            }

            if ("YTH" == statusCode || "YWC" == statusCode) {
                // 已完成和已退回
                checkView.visibility = View.VISIBLE
                checkDescTv.text = XUtils.string().getNotNullValue(taskDetailEntity.taskVO.backDesc)
            } else {
                checkView.visibility = View.GONE
            }
        } else {
            reportRateBtn.visibility = View.GONE
            checkTaskBtn.visibility = View.GONE
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            taskDescTv.text = Html.fromHtml(taskDetailEntity.taskVO.description, Html.FROM_HTML_MODE_LEGACY)
        } else {
            taskDescTv.text = Html.fromHtml(taskDetailEntity.taskVO.description)
        }
        val flowList = taskDetailEntity.flowList
        if (flowList != null) {
            taskRateAdapter!!.data = flowList
//            taskRateAdapter!!.setOnSuperListener { _, itemData, _ ->
//
//            }
        }
    }
}